package expenager.project.two;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

public class transaction extends Activity {
	TextView textSign;
	Spinner spinPaymentMethod, spinIncomes, spinExpenses;
	RadioButton radioIncome, radioExpense;
	//DBCurrentSetting dbSetting = new DBCurrentSetting(this);
	
	@Override
	public void onCreate (Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.transaction);
		
		textSign = (TextView) findViewById(R.id.textViewSign);
		textSign.setText(getCurrency());
		
		spinPaymentMethod = (Spinner) findViewById(R.id.spinPaymentMethod);	
		spinPaymentMethod.setAdapter(getItemPaymentMethod());
		
		radioIncome = (RadioButton) findViewById(R.id.radioIncome);
		radioIncome.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				spinExpenses.setVisibility(View.GONE);
				spinIncomes.setVisibility(View.VISIBLE);
			}
		});
		
		radioExpense = (RadioButton) findViewById(R.id.radioExpense);
		radioExpense.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				spinIncomes.setVisibility(View.GONE);
				spinExpenses.setVisibility(View.VISIBLE);
			}
		});
		
		spinIncomes = (Spinner) findViewById(R.id.spinIncomes);	
		spinIncomes.setAdapter(getItemIncome());
		
		spinExpenses = (Spinner) findViewById(R.id.spinExpenses);	
		spinExpenses.setAdapter(getItemExpense());
		spinExpenses.setVisibility(View.GONE);
	}
	
	public String getCurrency() {
		DBAdapter db = new DBAdapter(this);
		db.open();
		Cursor c = db.getSettingSign();
		String sign = c.getString(0).toString();
		db.close();
		return sign;
	}
	
	public ArrayAdapter<String> getItemPaymentMethod() {
		DBAdapter db = new DBAdapter(this);
		List<String> listPaymentMethod = new ArrayList<String>();
		//listPaymentMethod.add("abc");
		//listPaymentMethod.add("def");
		//listPaymentMethod.add("ghi");
		db.open();
		Cursor c = db.getSettingPaymentMethod();
		for(int i=0;i<c.getCount();i++) {
			listPaymentMethod.add(c.getString(0).toString());
			
			if(i!=(c.getCount()-1)) {
				c.moveToNext();
				}
		}
		db.close();
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listPaymentMethod);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		return adapter;
	}
	
	public ArrayAdapter<String> getItemIncome() {
		DBAdapter db = new DBAdapter(this);
		List<String> listIncome = new ArrayList<String>();

		db.open();
		Cursor c = db.getSettingIncome();
		for(int i=0;i<c.getCount();i++) {
			listIncome.add(c.getString(0).toString());
			
			if(i!=(c.getCount()-1)) {
				c.moveToNext();
				}
		}
		db.close();
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listIncome);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		return adapter;
	}
	
	public ArrayAdapter<String> getItemExpense() {
		DBAdapter db = new DBAdapter(this);
		List<String> listExpense = new ArrayList<String>();
		
		db.open();
		Cursor c = db.getSettingExpense();
		for(int i=0;i<c.getCount();i++) {
			listExpense.add(c.getString(0).toString());
			
			if(i!=(c.getCount()-1)) {
				c.moveToNext();
				}
		}
		db.close();
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listExpense);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		return adapter;
	}
}
